<template>
	<view><u-action-sheet :show="show" :actions="actions" cancel-text="取消" z-index="9999" @close="onClose" @cancel="onClose" @select="onSelect" /></view>
</template>

<script>
export default {
	data() {
		return {
			// 这里是一些组件内部数据
			show: false,
			actions: [
				{
					name: '拨号',
					type: 1
				},
				{
					name: '复制',
					type: 2
				},
				{
					name: '添加到通讯录',
					type: 3
				}
			]
		};
	},
	props: {
		// 这里定义了innerText属性，属性值可以在组件使用时指定
		callShow: {
			type: Boolean,
			default: false
		},
		mobile: {
			type: [String, Number],
			default: ''
		}
	},
	watch: {
		callShow: {
			handler: function(val) {
				if (val) {
					uni.getSystemInfo({
						success: res => {
							this.show = true;
						}
					});
				} else {
					this.show = false;
				}
			},

			immediate: true,
			deep: true
		}
	},
	methods: {
		// 这里是一个自定义方法
		onClose() {
			this.$emit('onClose', {
				detail: false
			});
		},

		onSelect(e) {
			const { mobile } = this;

			if (e.detail.type == 1) {
				uni.makePhoneCall({
					phoneNumber: mobile
				});
			} else if (e.detail.type == 2) {
				const that = this;
				uni.setClipboardData({
					data: mobile,
					success: function(res) {
						uni.getClipboardData({
							success: function(res) {
								that.$emit('onClose', {
									detail: false
								});
								uni.hideLoading();
								uni.showToast({
									title: '手机号复制成功',
									icon: 'none'
								});
							}
						});
					}
				});
			} else {
				uni.showActionSheet({
					itemList: ['添加联系人'],
					success: function(res) {
						// 添加到手机通讯录
						uni.addPhoneContact({
							firstName: '数码回收网',
							//联系人姓名
							mobilePhoneNumber: mobile //联系人手机号
						});
					}
				});
			}
		}
	}
};
</script>
<style>
@import './call.css';
</style>
